<!DOCTYPE html>
<html lang="en">
<head>
    <title>商品详情</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css"/><!-- bootstrap -->
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/jquery-validation/jquery.validate.min.js"></script> <!-- jquery-validator -->
    <script type="text/javascript" src="/jquery-validation/localization/messages_zh.min.js"></script>
    <script type="text/javascript" src="/layer/layer.js"></script><!-- layer -->
    <script type="text/javascript" src="/js/md5.min.js"></script><!-- md5.js -->
    <script type="text/javascript" src="/js/common.js"></script><!-- common.js -->
    <script type="text/javascript" src="/js/socket.js"></script><!-- common.js -->
</head>
<body>

<div class="panel panel-default">
    <div class="panel-heading">秒杀商品详情</div>
    <div class="panel-body">
        <div id="userTip" style="display: none">
            <span> 您还没有登录，请<a href="login.html">登陆</a>后再操作<br/></span>
        </div>
        <span>没有收货地址的提示。。。</span>
    </div>
    <table class="table">
        <tr>
            <td>商品名称</td>
            <td colspan="3" id="goodName"></td>
        </tr>
        <tr>
            <td>商品图片</td>
            <td colspan="3"><img id="goodImg" width="200" height="200"/></td>
        </tr>
        <tr>
            <td>秒杀开始时间</td>
            <td id="startDate"></td>
            <td id="seckillTip"></td>
            <td>
                <img id="verifyCodeImg" onclick="changeVerifyImg()" style="display: none">
                <input id="verifyCode" style="display: none">
                <button class="btn btn-primary btn-block" type="button" onclick="getPath()" id="buyButton">立即秒杀</button>
            </td>
        </tr>
        <tr>
            <td>商品原价</td>
            <td colspan="3" id="goodPrice"></td>
        </tr>
        <tr>
            <td>秒杀价</td>
            <td colspan="3" id="seckillPrice"></td>
        </tr>
        <tr>
            <td>库存数量</td>
            <td colspan="3" id="stockCount"></td>
        </tr>
    </table>
</div>
<script>
    var goodId;
    //距离秒杀开始剩余时间
    var startRemainSeconds;
    //距离秒杀结束剩余时间
    var endRemainSeconds;
    var timer;
    var user;
    var uuid;
    var socket;
    var realPath;
    var verifyCode;
    $(function () {
        goodId = getQueryString("goodId");
        if(!goodId){
            layer.msg("请正确操作");
            return;
        }
        //发送请求获取商品详细数据
        getDetail();
    });
    function getDetail() {
        $.ajax({
            url: '/goods/to_detail?goodId=' + goodId,
            type: 'GET',
            success: function (data) {
                if (data.code == 200) {
                    user = data.data.user;
                    initUser(user);
                    render(data.data.good);
                    initTime(data.data.good.startDate, data.data.good.endDate);
                } else {
                    layer.msg(data.msg)
                }
            },
            error: function () {
                layer.msg("操作");
            }
        })
    }
    function initUser(user) {
        if (!user) {
            $("#userTip").show();
        }else {
            uuid = getUuid(user.id);
        }
    }

    function render(goodVo) {
        $("#goodName").html(goodVo.goodName);
        $("#goodImg").prop("src", goodVo.goodImg);
        $("#startDate").html(goodVo.startDate);
        $("#goodPrice").html(goodVo.goodPrice);
        $("#seckillPrice").html(goodVo.seckillPrice);
        $("#stockCount").html(goodVo.stockCount);
    }

    function initTime(startDate, endDate) {
        var now = new Date();
        var sDate = new Date(startDate);
        var eDate = new Date(endDate);
        startRemainSeconds = parseInt((sDate.getTime() - now.getTime()) / 1000);
        endRemainSeconds = parseInt((eDate.getTime() - now.getTime()) / 1000);
        show();
        timer = window.setInterval(show, 1000);
    }

    function show() {
        startRemainSeconds--;
        endRemainSeconds--;
        if (startRemainSeconds > 0) {
            //秒杀未开始  开始时间减当前时间还有剩余
            $("#seckillTip").html("秒杀倒计时: " + startRemainSeconds + "秒");
            $("#buyButton").prop("disabled", true);
        } else if (endRemainSeconds > 0) {
            clearInterval(timer);
            $("#seckillTip").html("秒杀中");
            $("#buyButton").prop("disabled", false);
            changeVerifyImg();
            $("#verifyCodeImg").show();
            $("#verifyCode").show();
        } else {
            clearInterval(timer);
            $("#seckillTip").html("秒杀已结束");
            $("#buyButton").prop("disabled", true);
        }
    }
    function getPath() {
        if(!user){
            layer.msg("需登录才能参与秒杀活动")
            return;
        }
        verifyCode = $("#verifyCode").val();
        $.ajax({
            url:'/seckill/getPath',
            data:{goodId:goodId,userId:user.id,verifyCode:verifyCode},
            success:function (data) {
                if(data.code == 200){
                    realPath = data.msg;
                    doSeckill(realPath);
                }else{
                    layer.msg(data.msg);
                }
            },
            error:function () {
                layer.msg("客户端调用异常");
            }

        })
    }
    function doSeckill(realPath) {
        console.log(uuid);
        //加载中浮窗
        showLoading();
        $.ajax({
            url:'/seckill/'+realPath+'/do_seckill',
            type:'POST',
            data:{goodId:goodId,uuid:uuid},
            success:function (data) {
                if(data.code == 200){
                    if(socket==null){
                        socket = createScoket(uuid);
                    }
                }else {
                    layer.msg(data.msg);
                }
            },
            error:function () {
                layer.msg("客户端调用异常");
            }
        })
    }
    function changeVerifyImg() {
        $("#verifyCodeImg").prop("src","/getVerifyCodeImg?goodId="+goodId+"&"+new Date().getTime());
    }

</script>
</body>
</html>
